/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月4日
 */
package com.massyframework.beanskin.runtime;

import java.util.concurrent.ExecutorService;

/**
 * {@link ExecutorServiceAware} 提供{@link #setExecutorService(ExecutorService)}方法注入执行服务。
 */
public interface ExecutorServiceAware {

    /**
     * 设置执行线程池
     * @param executorService {@link ExecutorService}
     */
    void setExecutorService(ExecutorService executorService);

    /**
     * 尝试向<code>target</code>绑定<code>executorService</code>
     * @param target {@link Object}, 目标对象
     * @param executorService {@link ExecutorService}, 执行线程池
     * @return {@link boolean}, 返回<code>true</code>表示<code>target</code>支持{@link ExecutorServiceAware}接口，
     * 并调用{@link ExecutorServiceAware#setExecutorService(ExecutorService)}方法； 否则，返回<code>false</code>.
     */
    static boolean maybeToBind(Object target, ExecutorService executorService){
        if (target == null) return false;

        if (target instanceof ExecutorServiceAware){
            ((ExecutorServiceAware)target).setExecutorService(executorService);
            return true;
        }

        return false;
    }
}

